﻿
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;

using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Options;


namespace CarparkApi.Controllers
{
    public class PlateRecognizeController : Controller
    {
        private HttpContextAccessor _accessor;

        public PlateRecognizeController(HttpContextAccessor accessor)
        {
            _accessor = accessor;
        }
        [HttpPost]
        [Route("PlateReturn")]
        public async Task<IActionResult> PlateReturn([FromBody] VAXTORJson model)
        {

            //var request = _accessor.HttpContext.Request;
            //request.Body.Position = 0;
            //var buffer = new byte[Convert.ToInt32(request.ContentLength)];
            //await request.Body.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false);
            //var bodyJson = Encoding.UTF8.GetString(buffer);
            //if (string.IsNullOrEmpty(bodyJson))
            //{
            //    return Json(new { SUCCESS = false, message = "NO BODY JSON" });
            //}

            //model = JsonConvert.DeserializeObject<VAXTORJson>(bodyJson);  //==========================

            if (model == null)
            {
                return Json(new { SUCCESS = false, message = "PORSE VAXTOR JSON FAIL" });
            }
            else
            {
                //for log to console
                var modellog = model;
                modellog.image64 = modellog.image64.Substring(0, 20);
                var modellogStr = JsonConvert.SerializeObject(modellog);
                Console.WriteLine(modellogStr);
            }

            if (model != null)
            {
                DateTime OccuredDatetime = DatetimeHelper.ParseOccuredDatetime(model.date);

                string modelLogImage64Short = $"Image/Base64:{model.image64}......";
                if (model.image64.Length > 200)
                {
                    modelLogImage64Short = $"Image/Base64:{model.image64.Substring(0, 200)}......";
                }
                string log1 = $"[FUNC:plateReturn {DateTime.Now:yyyy-MM-dd HH:mm:ss fff}] [plate={model.plate}, ip={model.ip}, date={model.date}, confidence={model.confidence}, country={model.country}, OccuredDatetime={OccuredDatetime:yyyy-MM-dd HH:mm:ss fff}, ParkingNoCalcType={model.ParkingNoCalcType}]";
                Console.WriteLine(log1);
                return Json(new { SUCCESS = true, message = $"Success!!!{DateTime.Now:yyyy-MM-dd HH:mm:ss fff}" });
            }
            else
            {
                return Json(new { SUCCESS = false, message = "LOG SAVE FAIL!!!" });
            }
        }

    }
    public class VAXTORJson
    {
        public string plate { get; set; }
        public string date { get; set; }
        public string country { get; set; }
        public string confidence { get; set; }
        public string ip { get; set; }
        public string image64 { get; set; }
        public int ParkingNoCalcType { get; set; }

    }
}
